﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace brand.cyt369.com.MODELS
{
    public static class IQueryCollectionExtention
    {
        public static string GetString(this Microsoft.AspNetCore.Http.IQueryCollection query, string name)
        {
            var s = query[name];
            if (s.Count > 0) { return s[0].Trim(); }
            return s;
        }

        public static char? GetChar(this Microsoft.AspNetCore.Http.IQueryCollection query, string name)
        {
            string str = query[name];
            if (string.IsNullOrEmpty(str))
            {
                return null;
            }
            else
            {
                return str.ElementAt(0);
            }
        }

        public static char GetChar(this Microsoft.AspNetCore.Http.IQueryCollection query, string name, char defaultValue)
        {
            string str = query[name];
            if (string.IsNullOrEmpty(str))
            {
                return defaultValue;
            }
            else
            {
                return str.ElementAt(0);
            }
        }

        public static int[] GetIntegerArray(this Microsoft.AspNetCore.Http.IQueryCollection query, string name, char[] separator)
        {
            return GetIntegerList(query, name, separator).ToArray();
        }

        public static List<int> GetIntegerList(this Microsoft.AspNetCore.Http.IQueryCollection query, string name, char[] separator)
        {
            var array = new List<int>();

            string str = query[name];
            if (!string.IsNullOrEmpty(str))
            {
                var sps = str.Split(separator, StringSplitOptions.RemoveEmptyEntries);
                int num = 0;
                foreach (var s in sps)
                {
                    if (int.TryParse(s, out num))
                    {
                        array.Add(num);
                    }
                }
            }

            return array;
        }

        public static int? GetInteger(this Microsoft.AspNetCore.Http.IQueryCollection query, string name)
        {
            int num = 0;
            if (int.TryParse(query[name], out num))
            {
                return num;
            }

            return null;
        }

        public static int GetInteger(this Microsoft.AspNetCore.Http.IQueryCollection query, string name, int defaultValue)
        {
            int num = 0;
            if (int.TryParse(query[name], out num))
            {
                return num;
            }

            return defaultValue;
        }

        public static long? GetLong(this Microsoft.AspNetCore.Http.IQueryCollection query, string name)
        {
            long num = 0;
            if (long.TryParse(query[name], out num))
            {
                return num;
            }

            return null;
        }

        public static long GetLong(this Microsoft.AspNetCore.Http.IQueryCollection query, string name, long defaultValue)
        {
            long num = 0;
            if (long.TryParse(query[name], out num))
            {
                return num;
            }

            return defaultValue;
        }

        public static float? GetFloat(this Microsoft.AspNetCore.Http.IQueryCollection query, string name)
        {
            float num = 0;
            if (float.TryParse(query[name], out num))
            {
                return num;
            }

            return null;
        }

        public static float GetFloat(this Microsoft.AspNetCore.Http.IQueryCollection query, string name, float defaultValue)
        {
            float num = 0;
            if (float.TryParse(query[name], out num))
            {
                return num;
            }

            return defaultValue;
        }

        public static double? GetDouble(this Microsoft.AspNetCore.Http.IQueryCollection query, string name)
        {
            double num = 0;
            if (double.TryParse(query[name], out num))
            {
                return num;
            }

            return null;
        }

        public static double GetDouble(this Microsoft.AspNetCore.Http.IQueryCollection query, string name, double defaultValue)
        {
            double num = 0;
            if (double.TryParse(query[name], out num))
            {
                return num;
            }

            return defaultValue;
        }

        public static byte? GetByte(this Microsoft.AspNetCore.Http.IQueryCollection query, string name)
        {
            byte num = 0;
            if (byte.TryParse(query[name], out num))
            {
                return num;
            }

            return null;
        }

        public static byte GetByte(this Microsoft.AspNetCore.Http.IQueryCollection query, string name, byte defaultValue)
        {
            byte num = 0;
            if (byte.TryParse(query[name], out num))
            {
                return num;
            }

            return defaultValue;
        }

        public static DateTime? GetDateTime(this Microsoft.AspNetCore.Http.IQueryCollection query, string name)
        {
            DateTime time = DateTime.Now;
            if (DateTime.TryParse(query[name], out time))
            {
                return time;
            }

            return null;
        }

        public static DateTime GetDateTime(this Microsoft.AspNetCore.Http.IQueryCollection query, string name, DateTime defaultValue)
        {
            DateTime time = DateTime.Now;
            if (DateTime.TryParse(query[name], out time))
            {
                return time;
            }

            return defaultValue;
        }

        public static decimal? GetDecimal(this Microsoft.AspNetCore.Http.IQueryCollection query, string name)
        {
            decimal num = 0;
            if (decimal.TryParse(query[name], out num))
            {
                return num;
            }

            return null;
        }

        public static decimal GetDecimal(this Microsoft.AspNetCore.Http.IQueryCollection query, string name, decimal defaultValue)
        {
            decimal num = 0;
            if (decimal.TryParse(query[name], out num))
            {
                return num;
            }

            return defaultValue;
        }

        public static bool? GetBoolean(this Microsoft.AspNetCore.Http.IQueryCollection query, string name)
        {
            bool b = false;
            if (bool.TryParse(query[name], out b))
            {
                return b;
            }

            return null;
        }

        public static bool GetBoolean(this Microsoft.AspNetCore.Http.IQueryCollection query, string name, bool defaultValue)
        {
            bool b = false;
            if (bool.TryParse(query[name], out b))
            {
                return b;
            }

            return defaultValue;
        }

        public static Guid? GetGuid(this Microsoft.AspNetCore.Http.IQueryCollection query, string name)
        {
            Guid g = Guid.Empty;
            if (Guid.TryParse(query[name], out g))
            {
                return g;
            }

            return null;
        }

        public static Guid GetGuid(this Microsoft.AspNetCore.Http.IQueryCollection query, string name, Guid defaultValue)
        {
            Guid g = Guid.Empty;
            if (Guid.TryParse(query[name], out g))
            {
                return g;
            }

            return defaultValue;
        }

        public static T GetEnumFromString<T>(this Microsoft.AspNetCore.Http.IQueryCollection query, string name, T defaultValue) where T : struct, IConvertible
        {
            T result = defaultValue;
            if (Enum.TryParse<T>(query[name], out result))
            {
                return result;
            }

            return defaultValue;
        }

        public static T GetEnumFromString<T>(this Microsoft.AspNetCore.Http.IQueryCollection query, string name, T defaultValue, bool ignoreCase) where T : struct, IConvertible
        {
            T result = defaultValue;
            if (Enum.TryParse<T>(query[name], ignoreCase, out result))
            {
                return result;
            }

            return defaultValue;
        }

        public static T GetEnumFromInteger<T>(this Microsoft.AspNetCore.Http.IQueryCollection query, string name, T defaultValue) where T : struct, IConvertible
        {
            T result = defaultValue;

            var integer = GetInteger(query, name);
            if (integer.HasValue)
            {
                result = (T)(object)integer.Value;
            }

            return result;
        }

    }

}
